import json

import requests
import logging

URL = 'https://oapi.dingtalk.com/robot/send?access_token=f4dd172c23ee11f21e396547b8f4c44b5022a2f4b4d7827d4d6b13aef08567fe'

log = logging.getLogger(__name__)


class Ding(object):
    def __init__(self, url, content=''):
        self.url = url
        self.content = content

    def send(self):
        log.info('send ding {} {}'.format(self.content, self.url))
        msgtype = 'text'
        values = {
            'msgtype': 'text',
            msgtype: {
                'content': self.content
            }
        }
        headers = {'Content-Type': 'application/json; charset=UTF-8'}
        values = json.dumps(values)
        res = requests.post(self.url, values, headers=headers)
        if res.status_code == 200:
            return 'ok'
        return res.text


if __name__ == '__main__':
    d = Ding(URL, 'Is@所有人')
    d.send()
